package Servlet.user;

import Dao.UserDao;
import Manage.User;
import Utils.mybatisUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class userinsertServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
       //获取表单发来的数据
        String username=request.getParameter("username");
        String phone=request.getParameter("phone");
        String sex=request.getParameter("sex");
        String address=request.getParameter("address");
        String num=request.getParameter("num");
        //将数据放入User对象中
        User user=new User();
        user.setNum(Integer.parseInt(num));
        user.setSex(sex);
        user.setPhone(phone);
        user.setAddress(address);
        user.setUsername(username);
        //用mybatis将数据放进数据库
        SqlSession session= mybatisUtils.getSqlSession();
        UserDao dao=session.getMapper(UserDao.class);
        if (!("".equals(dao.selectusername(username)))&&null!=dao.selectusername(username)) {
            //因为数据库中已经有该用户信息，所以是更新
            dao.UsersUpdate(user);
            response.getWriter().print("操作成功");
        }
        else {
            //因为数据库中还没有该用户信息，所以是插入
            dao.usersinsert(user);
            response.getWriter().print("操作成功");
        }
        session.commit();
        session.close();
    }
}
